using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;

namespace DAL
{
    public class DOANHNGHIEPDAL
    {
         private SqlConnection cn = null;
        public DOANHNGHIEPDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(DOANHNGHIEP dn)
        {
            SqlCommand cm = new SqlCommand("Doanh_nghiep_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@tendnghiep", dn.Tendnghiep);
            cm.Parameters.AddWithValue("@diachi", dn.Dia_chi);
            cm.Parameters.AddWithValue("@matkhau", dn.Matkhau);
            cm.Parameters.AddWithValue("@tendnhap", dn.Tendnhap);
            cm.Parameters.AddWithValue("@website", dn.Website);
            cm.ExecuteNonQuery();
        }
        public void Update(DOANHNGHIEP dn)
        {

            SqlCommand cm = new SqlCommand("Doanh_nghiep_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madn", dn.Madn);
            cm.Parameters.AddWithValue("@tendnghiep", dn.Tendnghiep);
            cm.Parameters.AddWithValue("@diachi", dn.Dia_chi);
            cm.Parameters.AddWithValue("@matkhau", dn.Matkhau);
            cm.Parameters.AddWithValue("@tendnhap", dn.Tendnhap);
            cm.Parameters.AddWithValue("@website", dn.Website);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("Doanh_nghiep_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madn", iD);
            cm.ExecuteNonQuery();
        }
        public Doanh_nghiepCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("Doanh_nghiep_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            Doanh_nghiepCollection doanhnghiepCollection = new Doanh_nghiepCollection();

            while (dr.Read())
            {
                DOANHNGHIEP doanhnghiep = new DOANHNGHIEP();
                doanhnghiep.Madn = int.Parse(dr.GetValue(0).ToString());
                doanhnghiep.Tendnghiep = dr.GetValue(1).ToString();
                doanhnghiep.Dia_chi = dr.GetValue(2).ToString();
                doanhnghiep.Matkhau = dr.GetValue(3).ToString();
                doanhnghiep.Tendnhap = dr.GetValue(4).ToString();
                doanhnghiep.Website = dr.GetValue(5).ToString();
                doanhnghiepCollection.Add(doanhnghiep);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return doanhnghiepCollection;
        }
    }
}
